package me.zhanshi123.pixelpaygui.util

import com.mojang.authlib.GameProfile
import com.mojang.authlib.properties.Property
import io.izzel.taboolib.util.lite.Materials
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.SkullMeta
import java.util.*

object Skulls {
    private val cache = mutableMapOf<String, ItemStack>()

    fun getTextureSkull(texture: String) = getTextureSkull(texture, Materials.PLAYER_HEAD.parseItem()!!)

    fun getTextureSkull(texture: String, item: ItemStack): ItemStack = cache.computeIfAbsent(texture) {
        val meta = item.itemMeta as SkullMeta
        val profile = GameProfile(UUID.randomUUID(), null)
        val field = meta.javaClass.getDeclaredField("profile")
        profile.properties.put("textures", Property("textures", texture))
        field.isAccessible = true
        field[meta] = profile
        item.itemMeta = meta
        return@computeIfAbsent item
    }

}